﻿using MediatR;
using ProductionLine.API.Application.Command.Car;
using ProductionLine.Domain.Car;
using ProductionLine.Domain.ProductLine;
using ProductionLine.ErrorCode;
using ProductionLine.Extensions;
using ProductionLine.Infrastructure.Interface;

namespace ProductionLine.API.Application.Handler.Car
{
    public class GetProductLineHandler : IRequestHandler<GetProductLineCommand, APIResult<List<ProductLineDTO>>>
    {
        IBaseRepository<ProductLineModel> productRepo;
        IBaseRepository<CarModel> carRepo;

        public GetProductLineHandler(IBaseRepository<ProductLineModel> productRepo, IBaseRepository<CarModel> carRepo)
        {
            this.productRepo = productRepo;
            this.carRepo = carRepo;
        }

        public Task<APIResult<List<ProductLineDTO>>> Handle(GetProductLineCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ProductLineDTO>> res=new APIResult<List<ProductLineDTO>>();
            var productline = productRepo.GetAll().Where(c => !c.IsDel);
            var cars = carRepo.GetAll().Where(c => !c.IsDel);
            var linq = from a in productline
                       join b in cars on a.CarId equals b.CarId
                       select new ProductLineDTO
                       {
                           LineId=a.LineId,
                           LineCode=a.LineCode,
                           LineName=a.LineName,
                           LineType=a.LineType,
                           CarId=b.CarId,
                           CarName=b.CarName
                       };
            res.Code = APIEnum.Success;
            res.Data=linq.ToList();
            res.Msg = "查询生产线信息成功";
            return Task.FromResult(res);
        }
    }
}
